﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ArkanoidX.Objects
{
    public class MultiBall : Ball
    {
        public Ball[] SubBalls = null;

        public override bool IsDead
        {
            get
            {
                foreach (Ball ball in SubBalls)
                {
                    if (ball != null && ball._lifeState != Ball.LifeState.Dead)
                        return false;
                }
                return true;
            }
        }

        public MultiBall(Game game, Rectangle arena, Bricks bricks, Racket racket, int count)
            : base(game, arena, bricks, racket)
        {
            SubBalls = new Ball[count];
            for (int i = 0; i < count; i++)
            {
                SubBalls[i] = new Ball(game, arena, bricks, racket);
            }
        }

        public MultiBall(Ball ball, int count)
            : base(ball.Game, ball._arena, ball._bricks, ball._racket)
        {
            SubBalls = new Ball[count];
            for (int i = 0; i < count; i++)
            {
                SubBalls[i] = new Ball(ball);
            }
        }

        public override void Update(GameTime gameTime)
        {
            foreach (Ball ball in SubBalls)
            {
                ball.Update(gameTime);
                if (ball._lifeState == Ball.LifeState.Dead)
                    ball.RemoveFromGame();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < SubBalls.Length; i++)
            {
                if (SubBalls[i] != null && !SubBalls[i].IsDead)
                {
                    SubBalls[i].Draw(gameTime);
                }
            }
        }

        public Ball GetAlive()
        {
            foreach (Ball ball in SubBalls)
            {
                if (ball._lifeState != Ball.LifeState.Dead)
                {
                    return ball;
                }
            }

            return this.SubBalls[0];
        }

        public override void RemoveFromGame()
        {
            foreach (Ball ball in SubBalls)
            {
                ball.RemoveFromGame();
            }
            base.RemoveFromGame();
        }
    }
}
